package cn.psvmc.cxzapi.controller.store;

import cn.psvmc.cxzapi.model.common.*;
import cn.psvmc.cxzapi.model.store.TCoupon;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.store.TCouponService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 优惠券管理
 * @Author auto
 * @Date 2024-08-15
 */
@RestController
@RequestMapping("/coupon")
public class TCouponController {

    @Resource
    private TCouponService tCouponService;

    /**
    * @Author auto 2024-08-15 新增优惠券表
    * @Param tCoupon 优惠券表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TCoupon tCoupon) {
        ToolUtils.validation(new Object[]{
                tCoupon.getName(), "优惠券名称", 2
                , tCoupon.getCouponType(), "优惠券类型", 1
                , tCoupon.getCategory(), "优惠券品类", 1
                , tCoupon.getUseCondition(), "使用条件", 1
                , tCoupon.getNum(), "发放数量", 1
                , tCoupon.getUserScope(), "用户范围", 1
                , tCoupon.getBelongType(), "归属类型", 1
        });
        tCouponService.add(tCoupon);
        return ResultGenerator.genSuccessResult("保存成功！");
    }

    /**
    * @Author auto 2024-08-15 更新优惠券表
    * @Param tCoupon 优惠券表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TCoupon tCoupon) {
        ToolUtils.validation(new Object[]{tCoupon.getCouponid(), "id", 1, tCoupon.getRversion(), "版本", 1});
        int updateResult = tCouponService.edit(tCoupon);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
     * @Description 修改数量
     * @Date 2024/11/3 8:53
     * @Author YWX
     * @Param [tCoupon]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/edit_num")
    public Result editNum(@RequestBody TCoupon tCoupon) {
        Integer couponid = tCoupon.getCouponid();
        Integer rversion = tCoupon.getRversion();
        Integer num = tCoupon.getNum();
        ToolUtils.validation(new Object[]{couponid, "id", 1, rversion, "版本", 1, num, "数量", 1});
        tCouponService.editNum(couponid, rversion, num);
        return ResultGenerator.genSuccessResult("更新成功！");
    }

    /**
    * @Author auto 2024-08-15 查询优惠券表详情
    * @Param entity
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tCouponService.detail(id));
    }

    /**
     * @Description 删除优惠券
     * @Date 2024/11/3 9:14
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        tCouponService.delete(id, rversion);
        return ResultGenerator.genSuccessResult("删除成功！");
    }

    /**
     * @Description 下架
     * @Date 2024/11/3 9:10
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/down")
    public Result down(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        tCouponService.updateCouponStatus(id, rversion, 2);
        return ResultGenerator.genSuccessResult("下架成功！");
    }

    /**
     * @Description 生效
     * @Date 2024/8/20 16:06
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/up")
    public Result up(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        tCouponService.updateCouponStatus(id, rversion, 1);
        return ResultGenerator.genSuccessResult("上架成功！");
    }

    /**
    * @Author auto 2024-08-15 商家-优惠券列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public PageResult2 list(@RequestBody SearchEntity entity) {
        Page<TCoupon> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tCouponService.list(queryPage, entity));
    }

    /**
     * @Description 平台-优惠券列表
     * @Date 2024/8/19 9:57
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_pt")
    public PageResult2 listPt(@RequestBody SearchEntity entity) {
        Page<TCoupon> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tCouponService.list(queryPage, entity));
    }
}
